package com.mzmarket.market.module.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;

public final class JwtCookieUtil {
    private static final Key KEY = Keys.secretKeyFor(SignatureAlgorithm.HS512); //256-bit
    /** 加密：把任意字符串 → JWT */
    public static String encrypt(String raw) {
        return Jwts.builder()
                .setSubject(raw) // 整个字符串放 subject
                .setIssuedAt(new Date())
                .signWith(KEY)
                .setExpiration(new Date(System.currentTimeMillis() + 3600_000))
                .compact();
    }
    /** 解密：把 JWT → 原字符串 */
    public static Claims decrypt(String jwt) {
        return Jwts.parserBuilder()
                .setSigningKey(KEY)
                .build()
                .parseClaimsJws(jwt)
                .getBody()
                .setExpiration(new Date(System.currentTimeMillis() + 3600_000));
    }
}